package com.daidai.activitys

import android.Manifest
import android.content.Intent
import android.graphics.PixelFormat
import android.net.Uri
import android.os.Bundle
import android.provider.Settings
import android.view.Gravity
import android.view.MotionEvent
import android.view.View
import android.view.WindowManager
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.daidai.tools.G
import com.daidai.spinner.R

class TestWindowActivity : AppCompatActivity() {

    companion object{
        const val sys_alert_window = 111
    }

    private val permsSd = arrayOf(
//            Manifest.permission.WRITE_EXTERNAL_STORAGE,
//            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.SYSTEM_ALERT_WINDOW
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test_window)

        findBtn(R.id.btnShow1).setOnClickListener {
//            if(ActivityCompat.checkSelfPermission(this,Manifest.permission.SYSTEM_ALERT_WINDOW) !=
//                    PackageManager.PERMISSION_GRANTED){
//                elog("准备请求授权")
//                 ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.SYSTEM_ALERT_WINDOW),sys_alert_window)
//            }else{
//                //
//                elog("有了授权")
//                showPopWindow()
//            }
            if (Settings.canDrawOverlays(this)){
                G.elog("===直接显示")
                showPopWindow()
            }else{
                G.elog("===跳转设置")
                val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
                intent.data = Uri.parse("package:$packageName")
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                startActivityForResult(intent, sys_alert_window)
            }
        }


        findBtn(R.id.btnShowLog).setOnClickListener {
            G.elog("btn2 clicked!")
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when(requestCode){
            sys_alert_window ->{
                if (Settings.canDrawOverlays(this)){
                    G.elog("Nice,可以了")
                    showPopWindow()
                }else{
                    G.elog("还是不行")
                }
            }
        }
    }

     private fun showPopWindow(){
        val btn = Button(this)
        btn.text = " I a window btn!"



        // 设置 宽 高  x y  format flags gravity
        //  type 参数
        val params = WindowManager.LayoutParams()
        params.width = WindowManager.LayoutParams.WRAP_CONTENT
        params.height = WindowManager.LayoutParams.WRAP_CONTENT
        params.x = 100
        params.y = 300
        params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
        params.format = PixelFormat.TRANSPARENT
        params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        params.gravity = Gravity.LEFT or Gravity.TOP
        windowManager.addView(btn, params)

         btn.setOnTouchListener { _, event ->
             val rawX = event.rawX
             val rawY = event.rawY
             when(event.action){
                 MotionEvent.ACTION_MOVE->{
                     G.elog("move called!")
                     params.x = rawX.toInt()
                     params.y = rawY.toInt()
                     windowManager.updateViewLayout(btn,params)
                 }
             }
             return@setOnTouchListener false
         }

         btn.setOnClickListener {
             G.elog("popBtn clicked!")
             windowManager.removeViewImmediate(btn)
         }
    }


    fun findBtn(viewId: Int) = findViewById<View>(viewId)


//    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
//        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
//        when(requestCode){
//            sys_alert_window -> {
//                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//                    elog("获得了权限.....")
//                    showPopWindow()
//                }
//            }
//        }
//    }
}